package com.bootdo.coach.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.coach.domain.TabAgent;
import com.bootdo.coach.domain.TabChargeType;
import com.bootdo.coach.domain.TabCoach;
import com.bootdo.coach.domain.TabDevice;
import com.bootdo.coach.domain.TabDeviceType;
import com.bootdo.coach.domain.TabSellType;
import com.bootdo.coach.service.TabAgentService;
import com.bootdo.coach.service.TabChargeTypeService;
import com.bootdo.coach.service.TabCoachService;
import com.bootdo.coach.service.TabDeviceService;
import com.bootdo.coach.service.TabDeviceTypeService;
import com.bootdo.coach.service.TabSellTypeService;
import com.bootdo.coach.vo.TabDeviceVo;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

import io.swagger.annotations.ApiOperation;

/**
 * 设备端
 * 
 * @author Administrator
 *
 */
@RequestMapping("/coach/device")
@Controller
public class DeviceController extends BaseController {
	private String prefix = "coach/device";
	// 设备=============>>>开始
	

	@Autowired
	private TabDeviceService deviceService;
	
	@Autowired
	private TabDeviceTypeService deviceTypeService;
	
	@Autowired
	private TabSellTypeService sellTypeService;
	
	@Autowired
	private TabCoachService coachService;

	@Autowired
	private TabAgentService agentService;
	
	@Autowired
	private TabChargeTypeService chargeTypeService;

	@RequiresPermissions("coach:device:device")
	@GetMapping("device")
	String device(Model model) {
		// 设备类型
		Map<String, Object> query = new HashMap<String, Object>();
		List<TabDeviceType> deviceTypeList = deviceTypeService.list(query);
		if (deviceTypeList == null) {
			deviceTypeList = new ArrayList<TabDeviceType>();
		}
		TabDeviceType deviceType = new TabDeviceType();
		deviceType.setDeviceTypeId(0);
		deviceType.setDeviceTypeName("请选择设备类型");
		deviceTypeList.add(0, deviceType);
		model.addAttribute("deviceTypeList", deviceTypeList);
		return prefix + "/device";
	}
	
	@RequiresPermissions("coach:device:deviceType")
	@GetMapping("deviceType")
	String deviceType(Model model) {
		return prefix + "/deviceType";
	}

	@ApiOperation(value = "获取设备列表", notes = "")
	@ResponseBody
	@GetMapping("/deviceList")
	@RequiresPermissions("coach:device:device")
	public PageUtils deviceList(@RequestParam Map<String, Object> params) {
		int total = 0;
		List<TabDeviceVo> deviceList = new ArrayList<TabDeviceVo>();
		// 找出当前用户的代理商
		String bg_account = getUser().getUsername();
		TabAgent curAgent = agentService.findAgent(bg_account, null);
		if (curAgent == null) {
			PageUtils pageUtil = new PageUtils(deviceList, total);
			return pageUtil;
		}
		// 查询列表数据
		if (params != null&&params.containsKey("remarks")&&!"".equals(params.get("remarks").toString())) {
			params.put("remarks", "%" + params.get("remarks").toString() + "%");
		}
		Query query = new Query( params );
		query.put("agent_id", curAgent.getAgentId());
		// 查询列表数据
		deviceList = deviceService.list(query);
		total = deviceService.count(query);
		PageUtils pageUtil = new PageUtils(deviceList, total);
		return pageUtil;
	}

	@RequiresPermissions("coach:device:deviceAdd")
	@Log("添加设备")
	@GetMapping("/deviceAdd")
	String deviceAdd(Model model) {
		Map<String, Object> query = new HashMap<String, Object>();
		// 设备类型
		List<TabDeviceType> deviceTypeList = deviceTypeService.list(query);
		if (deviceTypeList == null) {
			deviceTypeList = new ArrayList<TabDeviceType>();
		}
		TabDeviceType deviceType = new TabDeviceType();
		deviceType.setDeviceTypeId(0);
		deviceType.setDeviceTypeName("请选择");
		deviceTypeList.add(0, deviceType);
		model.addAttribute("deviceTypeList", deviceTypeList);
		//销售类型
		List<TabSellType> sellTypeList = sellTypeService.findSellTypeList();
		if (sellTypeList == null) {
			sellTypeList = new ArrayList<TabSellType>();
		}
		TabSellType sellType = new TabSellType();
		sellType.setSellTypeId(0);
		sellType.setSellTypeName("请选择");
		sellTypeList.add(0, sellType);
		model.addAttribute("sellTypeList", sellTypeList);
		//教练
		// 找出当前用户的代理商
		String bg_account = getUser().getUsername();
		TabAgent curAgent = agentService.findAgent(bg_account, null);
		List<TabCoach> coachList = null;
		if (curAgent != null) {
			coachList = coachService.findCoachList(curAgent.getAgentId());
		}
		if (coachList == null) {
			coachList = new ArrayList<TabCoach>();
		}
		TabCoach coach = new TabCoach();
		coach.setCoachId(0l);
		coach.setCoachName("请选择");
		coachList.add(0, coach);
		model.addAttribute("coachList", coachList);
		//收费模式
		List<TabChargeType> chargeTypeList = chargeTypeService.findChargeTypeList();
		if (chargeTypeList == null) {
			chargeTypeList = new ArrayList<TabChargeType>();
		}
		TabChargeType chargeType = new TabChargeType();
		chargeType.setChargeTypeId(0);
		chargeType.setChargeTypeName("请选择");
		chargeTypeList.add(0, chargeType);
		model.addAttribute("chargeTypeList", chargeTypeList);
		
		return prefix + "/deviceAdd";
	}
	
	@ApiOperation(value = "获取设备类型列表", notes = "")
	@ResponseBody
	@GetMapping("/deviceTypeList")
	@RequiresPermissions("coach:device:deviceType")
	public PageUtils deviceTypeList(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<TabDeviceType> deviceTypeList = deviceTypeService.list(query);
		int total = deviceTypeService.count(query);
		PageUtils pageUtil = new PageUtils(deviceTypeList, total);
		return pageUtil;
	}
	
	@RequiresPermissions("coach:device:deviceTypeAdd")
	@Log("添加设备")
	@GetMapping("/deviceTypeAdd")
	String deviceTypeAdd(Model model) {
		return prefix + "/deviceTypeAdd";
	}

	@RequiresPermissions("coach:device:deviceEdit")
	@Log("编辑设备")
	@GetMapping("/deviceEdit/{deviceId}")
	String deviceEdit(Model model, @PathVariable("deviceId") Long deviceId) {

		Map<String, Object> query = new HashMap<String, Object>();
		// 设备类型
		List<TabDeviceType> deviceTypeList = deviceTypeService.list(query);
		if (deviceTypeList == null) {
			deviceTypeList = new ArrayList<TabDeviceType>();
		}
		TabDeviceType deviceType = new TabDeviceType();
		deviceType.setDeviceTypeId(0);
		deviceType.setDeviceTypeName("请选择");
		deviceTypeList.add(0, deviceType);
		model.addAttribute("deviceTypeList", deviceTypeList);
		//销售类型
		List<TabSellType> sellTypeList = sellTypeService.findSellTypeList();
		if (sellTypeList == null) {
			sellTypeList = new ArrayList<TabSellType>();
		}
		TabSellType sellType = new TabSellType();
		sellType.setSellTypeId(0);
		sellType.setSellTypeName("请选择");
		sellTypeList.add(0, sellType);
		model.addAttribute("sellTypeList", sellTypeList);
		//教练
		// 找出当前用户的代理商
		String bg_account = getUser().getUsername();
		TabAgent curAgent = agentService.findAgent(bg_account, null);
		List<TabCoach> coachList = null;
		if (curAgent != null) {
			coachList = coachService.findCoachList(curAgent.getAgentId());
		}
		if (coachList == null) {
			coachList = new ArrayList<TabCoach>();
		}
		TabCoach coach = new TabCoach();
		coach.setCoachId(0l);
		coach.setCoachName("请选择");
		coachList.add(0, coach);
		model.addAttribute("coachList", coachList);
		//收费模式
		List<TabChargeType> chargeTypeList = chargeTypeService.findChargeTypeList();
		if (chargeTypeList == null) {
			chargeTypeList = new ArrayList<TabChargeType>();
		}
		TabChargeType chargeType = new TabChargeType();
		chargeType.setChargeTypeId(0);
		chargeType.setChargeTypeName("请选择");
		chargeTypeList.add(0, chargeType);
		model.addAttribute("chargeTypeList", chargeTypeList);
		TabDevice device = deviceService.selectByPrimaryKey(deviceId);
		model.addAttribute("device", device);
		return prefix + "/deviceEdit";
	}

	@RequiresPermissions("coach:device:deviceAdd")
	@Log("保存设备")
	@PostMapping("/saveDevice")
	@ResponseBody
	R saveDevice(TabDevice device) {
		device.setCreateTime(new Date());
		if (device.getSellTypeId() == null || device.getSellTypeId() == 0) {
			return R.error("请选择销售类型");
		}
		if (device.getChargeTypeId() == null || device.getChargeTypeId() == 0) {
			return R.error("请选择收费模式");
		}
		if (device.getCoachId() == null || device.getCoachId() == 0l) {
			return R.error("请选择所属教练");
		}
		if (device.getDeviceTypeId() == null || device.getDeviceTypeId() == 0) {
			return R.error("请选择设备类型");
		}
		if (deviceService.insertSelective(device) > 0) {
			return R.ok();
		}
		return R.error();
	}
	
	@RequiresPermissions("coach:device:deviceTypeAdd")
	@Log("保存设备类型")
	@PostMapping("/saveDeviceType")
	@ResponseBody
	R saveDevice(TabDeviceType deviceType) {
		if (deviceTypeService.insertSelective(deviceType) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@RequiresPermissions("coach:device:deviceEdit")
	@Log("更新设备")
	@PostMapping("/updateDevice")
	@ResponseBody
	R updateDevice(TabDevice device) {
		if (device.getSellTypeId() == null || device.getSellTypeId() == 0) {
			return R.error("请选择销售类型");
		}
		if (device.getChargeTypeId() == null || device.getChargeTypeId() == 0) {
			return R.error("请选择收费模式");
		}
		if (device.getCoachId() == null || device.getCoachId() == 0l) {
			return R.error("请选择所属教练");
		}
		if (device.getDeviceTypeId() == null || device.getDeviceTypeId() == 0) {
			return R.error("请选择设备类型");
		}
		if (deviceService.updateByPrimaryKeySelective(device) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@RequiresPermissions("coach:device:deviceRemove")
	@Log("删除设备")
	@PostMapping("/removeDevice")
	@ResponseBody
	R removeDevice(Long deviceId) {
		if (deviceService.deleteByPrimaryKey(deviceId) > 0) {
			return R.ok();
		}
		return R.error();
	}
	
	@RequiresPermissions("coach:device:deviceTypeRemove")
	@Log("删除设备类型")
	@PostMapping("/removeDeviceType")
	@ResponseBody
	R removeDeviceType(Integer deviceTypeId) {
		if (deviceTypeService.deleteByPrimaryKey(deviceTypeId) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@PostMapping("/exitDeviceNo")
	@ResponseBody
	boolean exitDeviceNo(@RequestParam Map<String, Object> params) {
		// 存在，不通过，false
		return !deviceService.exitDeviceNo(params);
	}
	
	@PostMapping("/exitDeviceTypeName")
	@ResponseBody
	boolean exitDeviceTypeName(@RequestParam Map<String, Object> params) {
		// 存在，不通过，false
		return !deviceTypeService.exitDeviceTypeName(params);
	}

	// 设备=============>>>结束
}